package utils;

import javax.servlet.http.HttpServletRequest;


public class WebUtils {
	public static String safeGetParameterString(HttpServletRequest request, String parameterName){
		String value = request.getParameter(parameterName);
		if(value == null || "".equals(value.trim())){
			return null;
		}
		return value.trim();
	}
        
    public static Boolean safeGetParameterBoolean(HttpServletRequest request, String parameterName){
		String value = request.getParameter(parameterName);
		if(value == null || "".equals(value.trim())){
			return null;
		}
		return new Boolean(value.trim());
	}
	
	
	public static Long safeGetParameterLong(HttpServletRequest request, String parameterName){
		String value = request.getParameter(parameterName);
        if(value == null){
            return null;
        }
       	try {
			//flex valida como Long valido a: "1.", ".", ".0", "1.00", etc..
       		//No usar sin validador de flex por que redondea
			if(value != null && value.contains(".")){
				int index = value.indexOf('.');
				value = value.substring(0, index);
				if(value.length()==0)value = "0";
			}
			return new Long(value);
		} catch (NumberFormatException e) {
			return null;
		}
	}	
        public static Double safeGetParameterDouble(HttpServletRequest request, String parameterName){
		String value = request.getParameter(parameterName);
                if(value == null){
                    return null;
                }
                
		try {
			return new Double(value);
		} catch (NumberFormatException e) {
			return null;
		}
	}	
	
	public static Integer safeGetParameterInteger(HttpServletRequest request, String parameterName){
		String value = request.getParameter(parameterName);
        if(value == null){
            return null;
        }
		try {
			return new Integer(value);
		} catch (NumberFormatException e) {
			return null;
		}
	}	
}
